﻿// -----------------------------------------------------------------------
// <copyright file="PartJob_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class PartJob_DAL:BaseDAL
    {
        // Lấy tất cả JobPart
        public static List<JobPart> GetAll()
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.JobParts);
            return DB.JobParts.ToList();
        }
        // Lấy JobPart theo Id
        public static JobPart GetDataById(JobPart jp)
        {
            return DB.JobParts.Where(m => m.Parts_ID == jp.Parts_ID && m.Job_ID == jp.Job_ID).FirstOrDefault();
        }
        //Thêm một JobPart
        public static bool Insert(JobPart JobPart)
        {
            try
            {
                DB.JobParts.InsertOnSubmit(JobPart);
                DB.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }

        }
        // Xóa một JobPart
        public static bool Delete(JobPart JobPart)
        {
            JobPart p = GetDataById(JobPart);
            if (p != null)
            {
                DB.JobParts.DeleteOnSubmit(p);
                DB.SubmitChanges();
                return true;
            }
            return false;

        }
        public static bool DeleteByID(JobPart jp)
        {
            JobPart objJobPart = GetDataById(jp);
            return Delete(objJobPart);
        }
        // Cập nhật JobPart
        public static bool Update(JobPart JobPart)
        {
            try
            {
                //JobPart objJobPart = GetDataById(JobPart);
                //objJobPart.Job_ID = JobPart.Job_ID;
                //objJobPart.Parts_ID = JobPart.Parts_ID;
                //objJobPart.Quantity = JobPart.Quantity;
                DB.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }

        }
        public static bool isExits(JobPart jp)
        {
            JobPart objPartJob = null;
            objPartJob= DB.JobParts.Where(j => ((j.Job_ID == jp.Job_ID) &&
                (j.Parts_ID == jp.Parts_ID))).FirstOrDefault();
            return objPartJob != null ? true : false;
        }
        public static List<JobPart> GetDataByJobID(int id, bool ischanged)
        {
            if (ischanged)
            {
                DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.JobParts);
            }
            return DB.JobParts.Where(j => j.Job_ID == id).ToList();
        }
    }
}
